//package com.sui.cloud.cab.periodic.activity.ws.ms.mothlycard.order.controller;


// import com.kingdee.finance.commons.utils.json.JacksonUtils;
// import com.sui.cab.service.api.account.business.bo.PaymentCallBackBo;
// import com.sui.cab.service.api.account.business.bo.PaymentContentBo;
// import com.sui.cab.service.api.account.business.bo.RefundInfo;
// import com.sui.cloud.cab.code.enums.PayCallBackTypeEnum;
// import com.sui.cloud.cab.code.enums.PayOrderStatusEnum;
// import com.sui.cloud.cab.code.exceptions.ServiceException;
import org.springframework.security.crypto.codec.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * AES加解密
 * @author Wang Hom
 * @Date: Created on 17:23 2018/6/14
 */
public class EncryptUtils2 {

    private static final String ALGORITHM = "AES";

    private static final String MODE = "AES/ECB/PKCS5Padding";
    /**
     * AES加密
     * @param data
     * @param key
     * @return
     * @throws ServiceException
     */
    public static String encrypt(String data, String key) throws ServiceException {

        try {

            byte[] dataBytes = data.getBytes(String.valueOf(StandardCharsets.UTF_8));

            byte[] keyBytes = key.getBytes(String.valueOf(StandardCharsets.UTF_8));

            SecretKeySpec secretKey = new SecretKeySpec(keyBytes, ALGORITHM);

            byte[] enCodeFormat = secretKey.getEncoded();

            SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, ALGORITHM);

            Cipher cipher = Cipher.getInstance(MODE);

            cipher.init(Cipher.ENCRYPT_MODE, seckey);

            byte[] result = cipher.doFinal(dataBytes);

            return new String(Base64.encode(result));

        } catch (Exception e) {
            throw new ServiceException("encrypt fail!", e);

        }

    }



    /**
     * 解密
     * @param data
     * @param key
     * @return
     */
    public static String decrypt(String data, String key) throws ServiceException {

        try {

            byte[] dataBytes = Base64.decode(data.getBytes(String.valueOf(StandardCharsets.UTF_8)));

            byte[] keyBytes = key.getBytes(String.valueOf(StandardCharsets.UTF_8));

            SecretKeySpec secretKey = new SecretKeySpec(keyBytes, ALGORITHM);

            byte[] enCodeFormat = secretKey.getEncoded();

            SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, ALGORITHM);

            Cipher cipher = Cipher.getInstance(MODE);

            cipher.init(Cipher.DECRYPT_MODE, seckey);

            byte[] result = cipher.doFinal(dataBytes);

            return new String(result, String.valueOf(StandardCharsets.UTF_8));

        } catch (Exception e) {

            throw new ServiceException("decrypt fail!", e);

        }

    }

    public static void main(String[] args) {
        try {

            /**
             *
             * 			test5环境
             * 			账户中心：
             * 				payment.trade.salt：ClwAv2ZDHLzeleTk
             * 				payment.trade.aesKey: i+xdrftm6yXNVXXg
             * 			月卡中心：
             * 				payment.trade.salt：S87sNp4toQsP5/Pf
             * 				payment.trade.aesKey: b4AHimzQY9ZQqugc
             *
             *
             * 			test6环境
             * 			账户中心：
             * 				payment.trade.salt：6p65xZqDsjbdPL3B
             * 				payment.trade.aesKey: uSEViBNu16DlQlO4
             * 			月卡中心：
             * 				payment.trade.salt：9DX1Eok94cx14efi
             * 				payment.trade.aesKey: xtF8f61kM7fsxdTI
             *
             *
             * 			uat环境
             * 			账户中心：
             * 				payment.trade.salt：p58x206YYoeargKI
             * 				payment.trade.aesKey: /f0BjhR3qVDiZDQI
             * 			月卡中心：
             * 				payment.trade.salt：XMFG4o8YrXUl7JlD
             * 				payment.trade.aesKey: rF68yo13jAVObW6m
             *
             * 			run环境
             * 			账户中心 cab-account-center.yml
             * 				payment.trade.salt: 'vWTNBEdj1UrN7QLG'
             * 				payment.trade.aesKey: 'Rlly/E6Jyvc7KXDO'
             *
             *
             * 			月卡中心 cab-periodic-activity-center.yml
             * 				payment.trade.salt: 'zFwnfLg6fIwi+bu+'
             * 				payment.trade.aesKey: 's3kxAgwCT6b2c2TY'
             *
             *
             */

            // TODO 关键参数设置
            String orderOrderNo = "922532598963920897";
            String refundPlatformOrderNo = "PO2029254202409191655114890000";
            String salt = "zFwnfLg6fIwi+bu+";
            String aesKey = "s3kxAgwCT6b2c2TY";


            Long succeccAmount = 0L;
            long refundDate = new Date().getTime();

            RefundInfo refundInfo = new RefundInfo();
            refundInfo.setRefundDate(refundDate);
            refundInfo.setRefundReason("mock退款回调，手工回调");
            refundInfo.setOriginalOrderNo(orderOrderNo); // 需要设置的
            refundInfo.setRefundPlatformOrderNo(refundPlatformOrderNo);




            // 模拟回调数据
            PaymentContentBo paymentContentBo = new PaymentContentBo();
            // 回调的订单号设置
            paymentContentBo.setOrderNo("refund" + orderOrderNo);
            // 因为实际上不会去请求支付平台，所以这里不设置支付平台订单号
            paymentContentBo.setPlatformOrderNo(null);
            paymentContentBo.setOrderStatus(PayOrderStatusEnum.SUCCESS.getCode());
            paymentContentBo.setOrderFinishTime(refundDate);
            paymentContentBo.setSuccAmount(succeccAmount);
            paymentContentBo.setFailAmount(0L);
            paymentContentBo.setRefundInfo(refundInfo);
            PaymentCallBackBo paymentCallBackBo = PaymentCallBackBo.builder().type(PayCallBackTypeEnum.APP_PAY_REFUND.getCode()).content(paymentContentBo).build();

            String encrypt = EncryptUtils2.encrypt(JacksonUtils.jsonObjectSerializer(paymentCallBackBo) + salt, aesKey);
            // 请求接口地址
            System.out.println(encrypt);
        } catch (ServiceException e) {
            e.printStackTrace();
        }

    }

    private EncryptUtils2() {
        throw new IllegalStateException("Utility class");
    }
}
